package com.example.administrator.maizuofilm.notshow;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.maizuofilm.BaseFragment;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.NotShow;
import com.example.administrator.maizuofilm.information.InformationActivity;
import com.example.administrator.maizuofilm.movie.view.CustomRecyclerView;
import com.example.administrator.maizuofilm.movie.view.FullyLinearLayoutManager;
import com.example.administrator.maizuofilm.moviedetails.MovieDetailsActivity;
import com.example.administrator.maizuofilm.notshow.adapter.NotShowAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/7/12.
 */
public class NotShowFragment extends BaseFragment implements NotShowContract.NotShowView {
    @InjectView(R.id.recycler_notshow_movie)
    CustomRecyclerView recyclerNotshowMovie;
    private NotShowPresenter mNotShowPresenter;
    private NotShowModel mNotShowModel;
    private List<NotShow.DataBean.FilmListBean> notShows;
    private NotShowAdapter adapter;
    public static String filmID = "";
int currentIndex;//当前下标
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_notshow, null);
        ButterKnife.inject(this, view);
        mNotShowPresenter.init(currentIndex);
        FullyLinearLayoutManager fullyLinearLayoutManager = new FullyLinearLayoutManager(getActivity());
        fullyLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerNotshowMovie.setLayoutManager(fullyLinearLayoutManager);

        notShows = new ArrayList<>();
        adapter = new NotShowAdapter(notShows, getActivity());
        adapter.setNotShowListener(new NotShowAdapter.NotShowListener() {
            @Override
            public void onLayoutClick(View v, int position) {
                Intent intent = new Intent(getActivity(), MovieDetailsActivity.class);
                //  filmID=notShows.get(position).getFilmID();
                intent.putExtra("filmID", notShows.get(position).getFilmID());
                startActivity(intent);
            }

            @Override
            public void onMessageClick(View v, int position) {
                Intent intent = new Intent(getActivity(), InformationActivity.class);
                intent.putExtra("message", notShows.get(position).getInformation().getUrl());
                startActivity(intent);
            }
        });
        recyclerNotshowMovie.setAdapter(adapter);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNotShowModel = new NotShowModel();
        mNotShowPresenter = new NotShowPresenter(mNotShowModel, this);

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onStart() {
        super.onStart();

    }


    @Override
    public void onInitSuccess(NotShow notShow) {

        notShows.addAll(notShow.getData().getFilmList());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onInitFail(String failMsg) {
        Log.i("tag", failMsg);
    }

    @Override
    public void getIndexMore(int indexMore) {
        mNotShowPresenter.init(indexMore);
    }

    @Override
    public void getIndexRefresh(int indexRefresh) {
        notShows.clear();
        mNotShowPresenter.init(indexRefresh);
    }
}
